package com.example.myjpa;

import org.springframework.data.jpa.repository.support.SimpleJpaRepository;

import javax.persistence.EntityManager;
import javax.persistence.Query;

public class MyRepsoitoryImpl<T,ID> extends SimpleJpaRepository<T,ID> implements MyJpaRepository<T,ID> {

    EntityManager entityManager;
    Class<T> domainClass;

    public MyRepsoitoryImpl(Class<T> domainClass, EntityManager entityManager) {
        super(domainClass, entityManager);
        this.entityManager = entityManager;
        this.domainClass = domainClass;
    }

    @Override
    public T selectById(ID id) {
        String simpleName = domainClass.getSimpleName();
        Query query = entityManager.createQuery("select p from "+simpleName+" p where p.id = :id");
        query.setParameter("id",id);
        Object singleResult = query.getSingleResult();
        return (T) singleResult;
    }

}
